# -*- coding:utf-8 -*-
# @FileName  :vpn.py
# @Time      :2023/11/15 12:20
# @Author    :lin


import os
import re

routes = ['8.8.8.8', ]

txt = os.popen('route print 192.168.*')
txt = txt.read()
result = re.search(r' 192.*(10.204.*) (10.204.*) ', txt)
if result:
    gate = result.group(1).strip()
    inter = result.group(2).strip()
    print(f'网关：{gate} 接口：{inter}')

    txt = os.popen('route print')
    txt = txt.readlines()
    for line in txt:
        result = re.split('[ ]+', line.strip())
        if len(result) == 5 and result[2].strip() == gate and result[3].strip() == inter:
            para1 = result[0].strip()
            os.system(f'route delete {para1} {gate}')

    for route in routes:
        os.system(f'route add {route} mask 255.255.255.255 {gate}')
else:
    print('如果重复运行该程序，因主要网关已被清理将无法找到VPN网关，请尝试优化程序或重启VPN。')
